package com.kly.juc.threadpool;

import java.util.concurrent.*;

/**
 * @author admin
 * @date 2025/4/19
 */
public class ThreadPoolDemo1 {


    public static void main(String[] args) {
        createThreadPool();

    }

    /**
     * 创建线程池
     */
    public static void createThreadPool() {
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        Future<Integer> future = null;
        for (int i = 0; i < 10; i++) {
            future = executorService.submit(() -> {
                System.out.println("线程" + Thread.currentThread().getName() + "开始执行");
                return 0;
            });
        }
        try {
            Integer integer = future.get();
            System.out.println(integer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("线程池开始关闭");
        executorService.shutdown();

        FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("线程" + Thread.currentThread().getName() + "开始执行");
                return 0;
            }
        });

        // 自定义线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                10,
                1,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(100));
        CompletableFuture.supplyAsync(() -> {
            System.out.println("线程" + Thread.currentThread().getName() + "开始执行");
            return 0;
        }, threadPoolExecutor);


    }
}
